﻿using System;
using System.Collections; 
using Parseq.Core; 
using Parseq.Util; 

namespace Parseq.Compiler
{
    class TokenParser : BaseParser 
    {
        private BaseParser inner;
        private string tokenName; 
        public TokenParser(string tokenName, BaseParser inner)
        {
            this.tokenName = tokenName; 
            this.inner = inner; 
        }

        public TokenParser(string tokenName)
        {
            this.tokenName = tokenName; 
        }

        public BaseParser Inner
        {
            get { return this.inner; }
            set { this.inner = value; }
        }

        public override Result InnerParse(IReader reader, Location pos)
        {
            Result result = this.inner.Parse(reader, pos);
            if (Result.Succeeded(result))
            {
                return new Result(new Token(this.tokenName, Util.String.ArrayToString((Array)result.Inner)), result.Position);
            }
            else
            {
                return Result.MakeFail(reader, pos); 
            }
        }
        

    }
}
